package com.enjoy.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.enjoy.R;
import com.enjoy.commons.Commons;
import com.enjoy.po.Event;
import com.enjoy.utils.GPS;
import com.enjoy.utils.HttpPostHandle;
import com.enjoy.utils.PreferenceHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Event_list extends Activity {
	private List<Event> list;
	private ListAdapter adapter;
	ListView listView;
	Button choose;
	Button nextpage;
	Button add;
	HttpPostHandle httphandle;
	int type = 0;
	int page=0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.event_list);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork()
		.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
		.detectLeakedSqlLiteObjects() 
		.penaltyLog().penaltyDeath().build());
		
		
		listView = (ListView) findViewById(R.id.event_list_lv);
		choose = (Button) findViewById(R.id.event_list_bt_choose);
		nextpage = (Button) findViewById(R.id.event_list_bt_nextpage);
		choose = (Button) findViewById(R.id.event_list_bt_choose);

		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Event Resource = new Event();
				Resource = (Event) parent.getItemAtPosition(position);

				Intent intent = new Intent();
				intent.putExtra("eid", Resource.getEid() + "");

				// intent.setClass(Classify_ListView.this, MerchantInfo.class);
				// startActivity(intent);
			}

		});

		choose.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				LayoutInflater inflater = getLayoutInflater();
				final View layout = inflater.inflate(
						R.layout.event_list_dialog,
						(ViewGroup) findViewById(R.id.event_list_dialog));

				Button all = (Button) layout
						.findViewById(R.id.event_list_dialog_all);
				Button allbystatus = (Button) layout
						.findViewById(R.id.event_list_dialog_all_bystauts);
				Button mineButton = (Button) layout
						.findViewById(R.id.event_list_dialog_mine);
				Button myjoinButton = (Button) layout
						.findViewById(R.id.event_list_dialog_myjoin);

				AlertDialog.Builder builder = new AlertDialog.Builder(
						Event_list.this).setView(layout);
				all.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						type = 0;
					}
				});

				allbystatus.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						type = 1;
					}
				});

				mineButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						type = 2;
					}
				});

				myjoinButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						type = 3;
					}
				});

				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								refresh();
								dialog.dismiss();
							}
						});
				builder.show();

			}

		});

		refresh();

	}

	public void refresh() {
		list = new ArrayList<Event>();

		PostHandle();

		JSONObject JSON;
		try {
			JSON = httphandle.getResponse();

			JSONArray array = JSON.getJSONArray("resultarray");
			Log.i("array", array.toString());

			for (int i = 0; i < array.length(); i++) {

				Event event = new Event();
				event.setEid(array.getJSONObject(i).getInt("eid"));
				event.setEname(array.getJSONObject(i).getString("ename"));
				event.setPlacename(array.getJSONObject(i)
						.getString("placename"));
				event.setDistance(array.getJSONObject(i).getInt("distance"));
				event.setStatus(array.getJSONObject(i).getString("status"));
				// event.setDistance(distance)

				list.add(event);

			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		adapter = new ListAdapter(this, list);

		listView.setAdapter(adapter);

	}

	public static class ViewHolder {
		TextView ename;
		TextView placename;
		TextView distance;
		TextView status;
	}

	class ListAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		private List<Event> list;

		public ListAdapter(Context context, List<Event> list) {
			inflater = LayoutInflater.from(context);
			this.list = list;
		}

		public int getCount() {

			return list.size();
		}

		public Object getItem(int position) {

			return list.get(position);
		}

		public long getItemId(int position) {

			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.event_list_item, null);
				holder = new ViewHolder();

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.ename = (TextView) convertView
					.findViewById(R.id.event_list_item_ename);
			holder.ename.setText(list.get(position).getEname());

			holder.placename = (TextView) convertView
					.findViewById(R.id.event_list_item_placename);
			holder.placename.setText(list.get(position).getPlacename());

			holder.distance = (TextView) convertView
					.findViewById(R.id.event_list_item_distance);
			holder.distance.setText(list.get(position).getDistance() + "");

			holder.status = (TextView) convertView
					.findViewById(R.id.event_list_item_status);
			holder.status.setText(list.get(position).getStatus() + "");

			return convertView;
		}

	}

	public void PostHandle() {

		Location location = GPS.getLocation(Event_list.this);
		
	
		Float longitude =(float) 31.234;
		Float latitude =(float) 121.413;

		if(location !=null){
			longitude=(float) location.getLongitude();
			latitude = (float) location.getLatitude();
			
		}
		
		
		if (type == 0) {

			httphandle = new HttpPostHandle(Commons.URL
					+ "serviceName=EventService&operation=searchbylocation");
//			httphandle.add("latitude", location.getLatitude() + "");
//			httphandle.add("longitude", location.getLongitude() + "");
			httphandle.add("latitude", longitude + "");
			httphandle.add("longitude", latitude + "");
			httphandle.add("page", page + "");
		}

		if (type == 1) {

			httphandle = new HttpPostHandle(Commons.URL
					+ "serviceName=EventService&operation=searchbystatus");
//			httphandle.add("latitude", location.getLatitude() + "");
//			httphandle.add("longitude", location.getLongitude() + "");
			httphandle.add("latitude", longitude + "");
			httphandle.add("longitude", latitude + "");
			httphandle.add("page", page + "");
			httphandle.add("uid", PreferenceHelper.getSetting(Event_list.this, "uid", ""));
		}

		if (type == 2) {

			httphandle = new HttpPostHandle(Commons.URL
					+ "serviceName=EventService&operation=getmine");
			httphandle.add("uid", PreferenceHelper.getSetting(Event_list.this, "uid", ""));
			httphandle.add("page", page + "");
		}

		if (type == 3) {

			httphandle = new HttpPostHandle(Commons.URL
					+ "serviceName=EventService&operation=getmyjoin");
			httphandle.add("uid", PreferenceHelper.getSetting(Event_list.this, "uid", ""));
			httphandle.add("page", page + "");
		}
	}
}